/*!
 * \file __hash_node_mng_find.c
 *
 * All Rights Reserved Copyright (C) 2014 broada,Co.,Ltd.\n
 *
 * \version 3.0
 * \author geyh
 * \date 2016/03/31
 *
 */
#include <CUnit/Basic.h>
#include <stdlib.h>

#include "frcomm.h"
#include "frmem.h"
#include "frlist.h"
#include "frproto_mng.h"
#include "apmtest_cunit.h"

#include "apmtest_comm.h"

static int __suite_init(void)
{
//    frmodule_mng_init();
    return 0;
}

static int __suite_cleanup(void)
{
//    frmodule_mng_destory();
    return 0;
}


typedef struct __st_proto_mng {
    int i_is_enable;            /* 指定协议时有效 FR_YES,FR_NO */
    unsigned int ui_ver;        /* 指定协议时有效,协议版本号(更新后 +1) */
    int i_ip_proto;             /* 6(IPPROTO_TCP) / 17(IPPROTO_UDP) */
    u_int ui_ip;                /* ip 地址 */
    u_short us_port_start;      /* port 开始 */
    u_short us_port_end;        /* port 结束 */

    Frlist_header st_hdr_module_mng; /* module链表 */
} Frproto_mng;

int __hash_node_mng_find(Frlist_node *p_st_node, const void *p_compare_data);
void __hash_node_mng_free(Frlist_node *p_st_node);

static void __case1(void)
{
    Frlist_node *p_st_node = NULL;
    int i_ret = 0;
    Frproto_mng *p_st_proto_mng_node = NULL;
    Frproto_mng *p_compare_data = NULL;

    //p_st_node
    //p_st_proto_mng_node
    p_st_proto_mng_node = (Frproto_mng *)frmalloc(sizeof(Frproto_mng));
    memset(p_st_proto_mng_node, 0x00, sizeof(Frproto_mng));
    p_st_proto_mng_node->i_ip_proto = 1;
    p_st_proto_mng_node->ui_ip = iptools_parse_ipaddr((char *)"192.168.1.1");
    p_st_proto_mng_node->us_port_start = 40;
    p_st_proto_mng_node->us_port_end = 80;

    //p_st_node
    p_st_node = frlist_node_create(p_st_proto_mng_node);

    //p_compare_data
    p_compare_data = (Frproto_mng *)frmalloc(sizeof(Frproto_mng));
    memset(p_compare_data, 0x00, sizeof(Frproto_mng));
    p_compare_data->i_ip_proto = 1;
    p_compare_data->ui_ip = iptools_parse_ipaddr((char *)"192.168.1.1");
    p_compare_data->us_port_start = 40;
    p_compare_data->us_port_end = 80;

    i_ret = __hash_node_mng_find(p_st_node, p_compare_data);

    CU_ASSERT_EQUAL(i_ret, 0);

    __hash_node_mng_free(p_st_node);
    frfree(p_st_node);
    frfree(p_compare_data);
    return;

}

static void __case2(void)
{
    Frlist_node *p_st_node = NULL;
    int i_ret = 0;
    Frproto_mng *p_st_proto_mng_node = NULL;
    Frproto_mng *p_compare_data = NULL;

    //p_st_node
    //p_st_proto_mng_node
    p_st_proto_mng_node = (Frproto_mng *)frmalloc(sizeof(Frproto_mng));
    memset(p_st_proto_mng_node, 0x00, sizeof(Frproto_mng));
    p_st_proto_mng_node->i_ip_proto = 1;
    p_st_proto_mng_node->ui_ip = iptools_parse_ipaddr((char *)"192.168.1.1");
    p_st_proto_mng_node->us_port_start = 40;
    p_st_proto_mng_node->us_port_end = 80;

    //p_st_node
    p_st_node = frlist_node_create(p_st_proto_mng_node);

    //p_compare_data
    p_compare_data = (Frproto_mng *)frmalloc(sizeof(Frproto_mng));
    memset(p_compare_data, 0x00, sizeof(Frproto_mng));
    p_compare_data->i_ip_proto = 1;
    p_compare_data->ui_ip = iptools_parse_ipaddr((char *)"192.168.1.1");
    p_compare_data->us_port_start = 20;
    p_compare_data->us_port_end = 60;

    i_ret = __hash_node_mng_find(p_st_node, p_compare_data);

    CU_ASSERT_EQUAL(i_ret, 0);

    __hash_node_mng_free(p_st_node);
    frfree(p_st_node);
    frfree(p_compare_data);
    return;

}

static void __case3(void)
{
    Frlist_node *p_st_node = NULL;
    int i_ret = 0;
    Frproto_mng *p_st_proto_mng_node = NULL;
    Frproto_mng *p_compare_data = NULL;

    //p_st_node
    //p_st_proto_mng_node
    p_st_proto_mng_node = (Frproto_mng *)frmalloc(sizeof(Frproto_mng));
    memset(p_st_proto_mng_node, 0x00, sizeof(Frproto_mng));
    p_st_proto_mng_node->i_ip_proto = 1;
    p_st_proto_mng_node->ui_ip = iptools_parse_ipaddr((char *)"192.168.1.1");
    p_st_proto_mng_node->us_port_start = 40;
    p_st_proto_mng_node->us_port_end = 80;

    //p_st_node
    p_st_node = frlist_node_create(p_st_proto_mng_node);

    //p_compare_data
    p_compare_data = (Frproto_mng *)frmalloc(sizeof(Frproto_mng));
    memset(p_compare_data, 0x00, sizeof(Frproto_mng));
    p_compare_data->i_ip_proto = 1;
    p_compare_data->ui_ip = iptools_parse_ipaddr((char *)"192.168.1.1");
    p_compare_data->us_port_start = 60;
    p_compare_data->us_port_end = 100;

    i_ret = __hash_node_mng_find(p_st_node, p_compare_data);

    CU_ASSERT_EQUAL(i_ret, 0);

    __hash_node_mng_free(p_st_node);
    frfree(p_st_node);
    frfree(p_compare_data);
    return;

}


static void __case4(void)
{
    Frlist_node *p_st_node = NULL;
    int i_ret = 0;
    Frproto_mng *p_st_proto_mng_node = NULL;
    Frproto_mng *p_compare_data = NULL;

    //p_st_node
    //p_st_proto_mng_node
    p_st_proto_mng_node = (Frproto_mng *)frmalloc(sizeof(Frproto_mng));
    memset(p_st_proto_mng_node, 0x00, sizeof(Frproto_mng));
    p_st_proto_mng_node->i_ip_proto = 1;
    p_st_proto_mng_node->ui_ip = iptools_parse_ipaddr((char *)"192.168.1.1");
    p_st_proto_mng_node->us_port_start = 40;
    p_st_proto_mng_node->us_port_end = 80;

    //p_st_node
    p_st_node = frlist_node_create(p_st_proto_mng_node);

    //p_compare_data
    p_compare_data = (Frproto_mng *)frmalloc(sizeof(Frproto_mng));
    memset(p_compare_data, 0x00, sizeof(Frproto_mng));
    p_compare_data->i_ip_proto = 1;
    p_compare_data->ui_ip = iptools_parse_ipaddr((char *)"192.168.1.1");
    p_compare_data->us_port_start = 100;
    p_compare_data->us_port_end = 140;

    i_ret = __hash_node_mng_find(p_st_node, p_compare_data);

    CU_ASSERT_EQUAL(i_ret, 1);

    __hash_node_mng_free(p_st_node);
    frfree(p_st_node);
    frfree(p_compare_data);
    return;

}


int add_suite___hash_node_mng_find(void)
{
    CU_pSuite pSuite = NULL;

    if ((pSuite = CU_add_suite("frproto_mng __hash_node_mng_find()", __suite_init, __suite_cleanup)) == NULL)
        return FR_FAIL;

    CU_add_test(pSuite, "case1", __case1);
    CU_add_test(pSuite, "case2", __case2);
    CU_add_test(pSuite, "case3", __case3);
    CU_add_test(pSuite, "case4", __case4);
//    CU_add_test(pSuite, "case5", __case5);

    return FR_SUCC;
}
